<?php
namespace app\Recommended\controller;
use think\Controller;
use think\Db;

class Index extends Logincheck{
	function index(){
		// 首页推荐
		$list0 = hot_select_tj(0);
		$list1 = hot_select_tj(1);
		$list2 = hot_select_tj(2);
		$list3 = hot_select_tj(3);
		$list4 = hot_select_tj(4);
		$list5 = hot_select_tj(5);
		$list6 = hot_select_tj(6);
		// 返回榜单
		$resbandan = getteabandang();
		$this->assign('resbandan',$resbandan);
		//返回茶类
        $retvals = getTeaCase();
        $this->assign('teacase', $retvals);
        //返回产区
        $retval = getTeaArea();
        $this->assign('teaarea', $retval);
		$this->assign('list', $list0);
		$this->assign('list1', $list1);
		$this->assign('list2', $list2);
		$this->assign('list3', $list3);
		$this->assign('list4', $list4);
		$this->assign('list5', $list5);
		$this->assign('list6', $list6);
		return $this->fetch();
	}

	 //返回某茶类对应的产地JSON数据
    function reTeaAreaJson($teacase_id)
    {
        // $data = $this->reTeaArea($teacase_id);
        $data = $this->reTeaArea($teacase_id);
        return json_encode($data);
    }

    function get_hot_array($teacase_id){
    	$res = get_hot_tj($teacase_id);
    	db('hot_search')->where('head',$teacase_id)->delete();
    	$sql = 'insert into hot_search(bangdan_id,teaarea_id,teacase_id,frequency,head,list) values';
    	if(count($res)<1){
    		// return $this->error("没有数据 获取失败");
            return 0 ;
    	}
    	foreach ($res as $key => $item) {
    		$sql.= "('".$item['bangdan_id']."','".$item['teaarea_id']."','".$item['teacase_id']."','".$item['click']."','".$item['teacase_id']."','".($key+1)."'),";
    	}
    	$sql = rtrim($sql,',');
    	// mp($sql);
    	Db::execute($sql);
    	// return $this->success("获取推荐成功");
        return 1;
    }

    // 测试
    function text(){
    	$res = $this->get_hot_array(5);
		return $res;
    }

    //返回某茶类对应的产地
    function reTeaArea($teacase_id)
    {
        $retval = db("case_area")->field("teaarea_id")->where("teacase_id",$teacase_id)->select();
        $areas = getTeaArea();
        $data = [];
        foreach ($retval as $ret) {
            foreach ($areas as $area) {
                if($ret["teaarea_id"]==$area["id"]){
                    $data[] = $area;
               }
           }
       }
       return $data;
    }

	function updaterecommended($bangdan_id,$teacase_id,$teaarea_id,$id){
		// echo "榜单：".$bangdan_id.",之前的排名：".$last_list.",现在的排名:".$list.",茶类：".$teacase_id.",茶区：".$teaarea_id.",推荐id".$id;
		// exit();
		if(!$id){
            return $this->error("修改错误");
        }
		$data = [
			'bangdan_id'=>$bangdan_id,
			'teaarea_id'=>$teaarea_id,
			'teacase_id'=>$teacase_id,
		];
		// 修改完成
		$res = db('hot_search')->where('Id',$id)->update($data);
        return $this->success("推荐修改成功");
		
	}

	function updatelist($list,$id){
		$last_list = 2;
		if(!$id)return 0;
		if($last_list!=$list){
			db('hot_search')->where('list',$list)->update(['list'=>$last_list]);
		}
		db('hot_search')->where('Id',$id)->update(['list'=> $list]);
		return 1;
	}

    // 上移
    function update_top_list($list,$id,$head){
        if(!$list||!$id)return 0;
        // 修改记录位置
        $listJ =  $list-1;
        // 把修改的词条找出来
        $last_search = db('hot_search')->where(['list'=>$listJ,'head'=>$head])->find();
        // 记录修改词条的当前推荐位置
        $last_list = $last_search['list'];
        db('hot_search')->where(['list'=>$listJ,'head'=>$head])->update(['list'=> $list]);

        db('hot_search')->where(['id'=>$id,'head'=>$head])->update(['list'=>$last_list]);
        return 1;

    }
    function update_botton_list($list,$id,$head){
        if(!$list||!$id)return 0;
        // 修改记录位置
        $listJ =  $list+1;
        // 把修改的词条找出来
        $last_search = db('hot_search')->where(['list'=>$listJ,'head'=>$head])->find();
        if(count($last_search)<1)return 0;
        // 记录修改词条的当前推荐位置
        $last_list = $last_search['list'];
        db('hot_search')->where(['list'=>$listJ,'head'=>$head])->update(['list'=> $list]);

        db('hot_search')->where(['id'=>$id,'head'=>$head])->update(['list'=>$last_list]);
        return 1;
    }

}
